<?php

namespace App\Http\Controllers;


use App\Http\Requests\UserInfoPost;
use App\Models\Album;
use App\Models\Group;
use App\Models\GroupJoin;
use App\Models\UserInfo;
use Illuminate\Http\Request;

class UserInfoController extends BaseController
{
    /**
     * 获取用户详情
     *
     * @return array
     */
    public function getUserInfo()
    {
        $info = $this->user->MUserInfo;
        $detail = $this->user->MuserDetail;
        return compact('info', 'detail');
    }

    public function userInfo(UserInfoPost $request)
    {
        $u = UserInfo::query()->where(['user_id' => $this->userId])->first();

        $nickname = $request->input('nickname');
        $avatar = $request->input('avatar');
        $intro = $request->input('intro');

        if ($u) {
            if ($nickname) {
                $u->nickname = $nickname;
            }

            if ($avatar) {
                $u->avatar = $avatar;
            }

            if ($intro) {
                $u->intro = $intro;
            }

            if ($u->save()) {
                return $this->jsonResultOk();
            }
        }

        return $this->jsonResultFail();
    }

    /**
     * 我的 专辑 圈子 列表
     * 用来填写文章时候获取的参数
     *
     * @return array
     */
    public function list()
    {
        $album = Album::query()->where(['user_id' => $this->userId, 'status' => 2])->get();
        $group = GroupJoin::query()->where(['user_id' => $this->userId, 'status' => 2])->get();
        foreach ($group as $item) {
            $item->info;
        }
        return compact('album', 'group');
    }
}
